<?php

namespace zhtfrontend\controllers;

use common\components\helpers\Fun;
use common\models\datas\MessageConfigItem;
use common\models\datas\MessageConfig;
use doumiaoduo\buildform\BuildForm;
use yii\web\Response;
use common\components\extenders\AjaxObject;
use zhtfrontend\components\bases\BaseController;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use common\components\actions\DeleteAction;
use yii\helpers\ArrayHelper;
use common\components\helpers\Util;

class MessageconfigController extends BaseController
{
    public function actionIndex()
    {
        $data = MessageConfigItem::find()->where(['site_id' => Fun::getSiteId()])->orderBy('id ASC')->all();
        $array = Fun::asArray($data, ['admin', 'config_name', 'frontend_operate', 'type_id']);

        return $this->render('index',['data'=>$array]);
    }

    public function actionCreate()
    {
        $this->layout = false;
        $model = new MessageConfigItem();
        $form = new BuildForm('@app/components/forms/messageconfigitemForm.php');
        $form->loadModel(['messageconfigitem' => $model]);
        $mechanic = MessageConfig::find()->where(['mode' => 2])->asArray()->all();
        $form->setItems('messageconfigitem', 'config_id', ArrayHelper::map($mechanic, 'id', 'name'));
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'attr') {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $id = \Yii::$app->request->post('configitem');
            $content = MessageConfig::find()->where(['id' => $id])->one();
            $ajax_object = new AjaxObject();
            $ajax_object->data = Util::array2text(unserialize($content->variable));
            return $ajax_object;
        }
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('messageconfigitem');
            $model->send_way = serialize($model->send_way);
            $model->site_id = Fun::getSiteId();
            $model->admin_id = \Yii::$app->user->id;
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['messageconfig/index']), true);
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'attr') {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $id = \Yii::$app->request->post('configitem');
            $content = MessageConfig::find()->where(['id' => $id])->one();
            $ajax_object = new AjaxObject();
            $ajax_object->data = Util::array2text(unserialize($content->variable));
            return $ajax_object;
        }
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('backend', '信息不存在'));
        }
        $model = MessageConfigItem::findOne(\Yii::$app->request->get('id'));
        if ($model == null) {
            throw new NotFoundHttpException(\Yii::t('backend', '信息不存在'));
        }
        $form = new BuildForm('@app/components/forms/messageconfigitemForm.php');
        $form->loadModel(['messageconfigitem' => $model]);
//        $mechanic=MessageConfig::find()->all();
        $mechanic = Fun::cache('messageconfig_cache', false);
        $form->setItems('messageconfigitem', 'config_id', ArrayHelper::map($mechanic, 'id', 'name'));
        $model->send_way = unserialize($model->send_way);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('messageconfigitem');
            $model->send_way = serialize($model->send_way);
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('backend', '修改成功'), Fun::url('messageconfig/index'), 'success');
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actions()
    {
        return [
            'delete' => [
                'class' => DeleteAction::className(),
                'modelClass' => MessageConfigItem::className(),
                'message' => ['success' => '删除成功'],
                'redirectTo' => 'messageconfig/index',
            ],
        ];
    }

}
